<?php

namespace app\service;


use app\model\DiamondShop;
use app\model\RoleShop;
use app\model\UserOrder;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

//钻石商店服务
class DiamondShopService extends BaseService
{


    protected function condition(): array
    {
        return [//where=>条件 random=>获取数量 limit=>限购
            1 => ['where' => 'today', 'random' => 1, 'limit' => 4], 'item' => [ //第1次免费领取、后续3次为看广告
                ['ptype' => GET_METHOD['免费'], 'price' => 0],
                ['ptype' => GET_METHOD['看广告'], 'price' => 1],
                ['ptype' => GET_METHOD['看广告'], 'price' => 1],
                ['ptype' => GET_METHOD['看广告'], 'price' => 1],
            ],
            4 => ['where' => 'forever', 'random' => 1, 'limit' => 1],
        ];
    }


    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function list($user_id): array
    {
        $diamond_shop_list = (new DiamondShop())->select();

        foreach ($diamond_shop_list as $diamond_shop) {

            //查询领取条件是否满足
            $count = (new UserOrder())->where('user_id', $user_id)->where('shop_type', SHOP_TYPE['钻石商店'])->where('shop_id', $diamond_shop->id);
            $condition = $this->condition();

            if (isset($condition[$diamond_shop->type])) {
                if ($condition[$diamond_shop->type]['where'] !== 'forever') {
                    $count->whereTime('created_at', $condition[$diamond_shop->type]['where']);
                }
                $count = $count->count();

                if ($condition[$diamond_shop->type]['limit'] <= $count) {
                    return $this->returnFail('领取次数已达上限');
                }

                if (isset($condition[$diamond_shop->type]['item'])) {
                    $item = $condition[$diamond_shop->type]['item'][$count];
                    $diamond_shop->ptype = $item['ptype'];
                    $diamond_shop->price = $item['price'];
                }

                $diamond_shop->purchased = $count;
                $diamond_shop->limit = $condition[$diamond_shop->type]['limit'];
            } else {
                $diamond_shop->purchased = -1;
                $diamond_shop->limit = -1;
            }


            $this->handleModelData($diamond_shop);
        }


        return $this->returnSuccess($diamond_shop_list->toArray());

    }


    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function buy($user_id, $diamond_shop_id): array
    {
        //获取鱼的类型

        $diamond_shop = (new RoleShop())->find($diamond_shop_id);

        if (!$diamond_shop) {
            return $this->returnFail('没有获取到商品');
        }


        //查询领取条件是否满足
        $count = (new UserOrder())->where('user_id', $user_id)->where('shop_type', SHOP_TYPE['钻石商店'])->where('shop_id', $diamond_shop_id);
        $condition = $this->condition();

        if ($condition[$diamond_shop->type]['where'] !== 'forever') {
            $count->whereTime('created_at', $condition[$diamond_shop->type]['where']);
        }
        $count = $count->count();

        if ($condition[$diamond_shop->type]['limit'] <= $count) {
            return $this->returnFail('领取次数已达上限');
        }

        if (isset($condition[$diamond_shop->type]['item'])) {
            $item = $condition[$diamond_shop->type]['item'][$count];
            $diamond_shop->ptype = $item['ptype'];
            $diamond_shop->price = $item['price'];
        }

        //如果是2类型 第1次免费领取、后续3次为看广告
        if (in_array($diamond_shop->ptype, USER_CAPITAL_TYPE)) {
            //扣除货币
            $res = (new UserService())->change_money($user_id, $diamond_shop->price, $diamond_shop->ptype, USER_CAPITAL_STATUS['扣除']);
            if (!$res['code']) {
                return $res;
            }
        }


        //材料发放---待定


        //生成订单
        $user_order = new UserOrder();
        $user_order->user_id = $user_id;
        $user_order->shop_id = $diamond_shop->id;
        $user_order->price = $diamond_shop->price;
        $user_order->ptype = $diamond_shop->ptype;
        $user_order->channel = 'diamond_shop';
        $user_order->shop_type = SHOP_TYPE['钻石商店'];
        $user_order->num = 1;
        $user_order->save();

        //去除多余字段
        $user_order = $this->handleModelData($user_order);
        return $this->returnSuccess($user_order->toArray());
    }


}